package cn.wgx.modules.sys.util;

import cn.wgx.commons.util.SpringContextHolder;
import cn.wgx.modules.sys.entity.SysChinaCity;
import cn.wgx.modules.sys.services.SysChinaCityService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Hashtable;
import java.util.Map;

/**
 * 城市工具类
 */
public class CityUtil {

    @Autowired
    private static SysChinaCityService sysChinaCityService;

    static {
        if(sysChinaCityService == null){
            sysChinaCityService = SpringContextHolder.getBean("sysChinaCityService");
        }
    }

    private static Map<Integer,String> city = new Hashtable<>();

    public static String getName(Integer id) {
        if(null == id){
            return null;
        }
        if(city.containsKey(id)){
            return city.get(id);
        }
        SysChinaCity sysChinaCity = sysChinaCityService.getOne(id);
        if(sysChinaCity != null){
            String name = sysChinaCity.getCityName();
            city.put(id, name);
            return name;
        }
        return null;
    }
}
